package cn.dhnapplets.service.impl;


import cn.dhnapplets.entity.Picture;
import cn.dhnapplets.entity.Result;
import cn.dhnapplets.mapper.tbPictureMapper;
import cn.dhnapplets.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 邱文彬
 * @Date 2021/11/5 14:20
 * @Version 1.0
 */


@Service
@Transactional
public class PictureServiceImpl implements PictureService {
    @Autowired
    private tbPictureMapper tbPictureMapper;


    @Override
    public int uploadPicture(Integer openId, String accountName, String avatar, String pictureName, String filename, String pictureTestUrl,String pictureDetails) {
        Picture picture = new Picture();
        picture.setPictureUrl(avatar);
        picture.setPictureState(2);
        picture.setPictureName(filename);

        picture.setPictureCreationTime(new Date());
        picture.setPictureOfficialAccountUrl(pictureTestUrl);

        picture.setPictureDetails(pictureDetails);
        int code = tbPictureMapper.pictureInsert(picture);
        return code;

    }

    @Override
    public List<Picture> getPicture() {

        List<Picture> quertyPicture = tbPictureMapper.quertyPicture();


        return quertyPicture;
    }

    @Override
    public int setExhibition(String pictureName) {
        Integer integer = tbPictureMapper.quertyExhibitionPictures();

        if(integer >= 2){
            return 0;
        }else{
            return tbPictureMapper.updatePictureByPictureName(pictureName);
        }
    }

    @Override
    public int getExhibition(String pictureName) {



            return tbPictureMapper.noUpdatePictureByPictureName(pictureName);

    }

    @Override
    public Map<String, Picture> getPictureAll() {
        List<Picture> quertyPicture = tbPictureMapper.quertyPictureAll();
        Map<String, Picture> map = new HashMap<>();
        for (Picture picture : quertyPicture) {
            map.put(picture.getPictureName(), picture);
        }
        return map;
    }

    @Override
    public Integer getTheNumberOfPictures() {
        Integer number = tbPictureMapper.quertyExhibitionPictures();
        return number;
    }

    @Override

    public Result setPictureState(String oldpictureName, String newpictureName) {



            Integer code1 = tbPictureMapper.updatePictureByPictureNameOld(oldpictureName);
            if (code1 > 0) {
                Integer code2 = tbPictureMapper.updatePictureByPictureName(newpictureName);
                if (code2 > 0) {
                    return new Result(0,"更新成功",null);
                } else {
                    return new Result(10002, "更新时出现错误", null);
                }
            } else {
                return new Result(10001, "更新时出现错误", null);
            }

        
    }

    @Override
    public Integer bindingUrl(String pictureName,String pictureTestUrl) {
        Integer code = tbPictureMapper.updatedinbingUrlByName(pictureName,pictureTestUrl);
        return code;
    }

    @Override
    public Integer deletePircture(String pictureName) {

        int i = tbPictureMapper.deletePicture(pictureName);
        return i;
    }
}
